 var pageParams;
function refresh() {
    $("#dataTable").bootstrapTable("removeAll");
    $("#dataTable").bootstrapTable('refresh');
}




function modifyCatalog(data) {
    var rows = $("#dataTable").bootstrapTable('getSelections');
    var rowIds = [];
    for (var i = 0; i < rows.length; i++) {
        rowIds.push(rows[i].id);
    }
    var catalogId=data.DataSourceID;
    if(catalogId==1){
        catalogId=-1;
    }
    $$.ajax({
        url: "/rest/IotCalcBusiness/modifyBatch",
        data: {
            catalogId: catalogId,
            id: rowIds.join(",")
        },
        success: function (data) {
            layer.msg('设置成功', {time: 2000});
        }
    });
    refresh();
}

function editData(rowId) {
    if(pageParams.pId){
        $$.goUrl('/iot/calc/calc_update.html?rowId=' + rowId+'&pId='+pageParams.pId+'&pCode='+pageParams.pCode);

    }else{
        $$.goUrl('/iot/calc/calc_update.html?rowId=' + rowId);
    }

}

//搜索以前将页码换成第一页
function searchInit() {
    var options = $("#dataTable").bootstrapTable("getOptions");
    options.pageNumber = 1;
    $("#dataTable").bootstrapTable('refreshOptions', options);
}

function initSearchEvent() {

    $("#serachBtn").click(function () {
        searchInit();
        refresh();
    });
    $('#serachBtn').bind('keypress', function (e) {


        if (e.keyCode == 13) {
            searchInit();
            refresh();
        }
    });
    $('#seachValue1').bind('keypress', function (e) {

        if (e.keyCode == 13) {
            searchInit();
            refresh();
        }
    });
    $('#seachValue2').bind('keypress', function (e) {

        if (e.keyCode == 13) {
            searchInit();
            refresh();
        }
    });
    $("#searchReset").on("click", function () {
        //$("#searchForm")[0].reset();
        $("#searchForm input[type='text']").val("");
    });
    $("#return-btn").click(function () {
        $$.goUrl("/iot/calc_rule/calc_rule_list.html?&pId="+pageParams.pId+"&pCode="+pageParams.pCode+"" );
    });
    $("#add-btn").click(function () {
        if(pageParams.pId){
            $$.goUrl("/iot/calc/calc_add.html?&pId="+pageParams.pId+"&pCode="+pageParams.pCode+"" );

        }else{
            $$.goUrl("/iot/calc/calc_add.html");
        }
     });


}






(function (undefined) {
    "use strict"
    var _global;

    function result(args, fn) {
        var argsArr = Array.prototype.slice.call(args);
        if (argsArr.length > 0) {
            return argsArr.reduce(fn);
        } else {
            return 0;
        }
    }

    var collectionList = {
        columnsInit: function () {
            return [ {
                field: 'status',
                title: '序号',
                formatter: function (value, row, index) {
                    return index + 1;
                }
            },
                {field: "stable", title: "源表格", sortable: true},
                {field: "scode", title: "源编码"},
                {field: "ttable", title: "目标表格"},
                {field: "tcode", title: "目标编码"},
                {field: "catalogName", title: "分类名称"},
                {field: "remarks", title: "备注"},
                {
                    field: 'status',
                    title: '状态',
                    formatter: function (value, row, index) {
                        if (value == '1') {
                            return [
                                '<span class="sys-type-bg pro-status-green">启用</span>'
                            ];
                        }
                        return [
                            '<span class="sys-type-bg pro-status-red">停用</span>'
                        ];

                    }

                },
                {
                    field: 'optionDel',
                    checkbox: true
                }, {
                    field: 'option',
                    title: '操作',
                    width: 80,
                    align: 'center',
                    formatter: function (value, row, index) {
                        /*//up菜单向上展示、 down向下展示
                         var pageSize = deviceList.pageSize;
                         var cssStyle = 'bottom';
                         if (index > pageSize / 3) {
                         cssStyle = 'top';
                         }*/
                        return [
                            '<a class="opt-icon" href="javascript:editData(\'' + row.id + '\')" title="编辑" >',
                            '<i class="glyphicon glyphicon-pencil"></i>',
                            '</a>  '
                            ].join('');
                    }

                }]
        },
        gridInit: function (columns) {
            return $("#dataTable").bootstrapTable({
                method: "post",
                contentType: "application/x-www-form-urlencoded",
                url: $$.restServerUrl + '/bootstrap/basegrid/IotCalcBusiness/list',
                columns: columns,
                undefinedText: '-',
                striped: 'true',
                classes: 'table table-hover qxttable',
                pagination: 'true',
                clickToSelect: true,
                singleSelect: false,
                sidePagination: "server",
                queryParamsType: "page",
                sortSide: 'client',
                sortable: true,
                pageNumber: 1,
                pageSize: 10,
                idField: 'id',
                silentSort: true,
                pageList: [
                    10, 20, 50, 100
                ],
                onClickRow: function (row, $element, field) {
                    $("#dataTable").bootstrapTable("uncheckAll");
                },
                queryParams: function queryParams(params) {   //设置查询参数

                    var args = {};
                    if(pageParams.pId){
                        args.c_refId_1=pageParams.pId;
                    }

                    args.c_isRpt_1=2;
                    args.c_stable_7 = $("#seachValue1").val();
                    args.c_scode_7 = $("#seachValue2").val();
                    args.page = params.pageNumber - 1; //    params.pageNumber
                    args.pageSize = params.pageSize;     //params.pageSize
                    args.isPage = 1;
                    //args.order=orderStr;
                    var paramStr = JSON.stringify(args);

                    //alert(params.pageNumber+":"+params.pageSize);
                    //alert(orderStr);
                    var param = {
                        params: paramStr
                    };
                    return param;
                },
                onPostBody: function () {
                    $(".opt-dropdown").each(function (index, data) {
                        var _this = this;
                        $(this).hover(function () {
                            $(_this).find(".opt-dropdown-menu").show();
                        }, function () {
                            $(_this).find(".opt-dropdown-menu").hide();
                        })
                    });
                    var minHeight = $("#rightCont").height() -30;
                    var maxheight = $("#dataTable").height() + 100;

                }
            })

        },
        dataInit: function () {
            return []
        }
    }

    // 将插件对象暴露给全局对象
    _global = (function () {
        return this || (0, eval)('this');
    }());
    if (typeof module !== "undefined" && module.exports) {
        module.exports = collectionList;
    } else if (typeof define === "function" && define.amd) {
        define(function () {
            return collectionList;
        });
    } else {
        !('plugin' in _global) && (_global.collectionList = collectionList);
    }
}());
$(function () {
    pageParams=$$.getQueryString();
    if(pageParams.pId){
        window.thirdMenuCode='MhRules';
    }

    initSearchEvent();
    if(pageParams.pId){
        $("#return-btn").show();
    }else{
        $("#return-btn").hide();
    }
    //右侧表格
    $('#deviceCode').focus();
    //重置表单
    $("#searchReset").on("click", function () {
        $("#searchForm")[0].reset();
    });
    //渲染表格
    var columns = collectionList.columnsInit();
    var table = collectionList.gridInit(columns);
    var datas = collectionList.dataInit();
    $("#dataTable").bootstrapTable("append", datas);
    //点击删除按钮信息弹出框
    $("#del-btn").click(function () {
        var len = $("#dataTable input:checked").length;
        if (len == 0) {
            //未选中数据提示框
            layer.msg('请选择数据', {time: 1000});
        } else {
            layer.confirm('确定要删除这条数据吗？',
                {icon: 3, title: '提示', area: '300px', btnAlign: 'c'}, function (index) {
                    var rows = $("#dataTable").bootstrapTable('getSelections');
                    var rowIds = [];
                    for (var i = 0; i < rows.length; i++) {
                        rowIds.push(rows[i].id);
                    }
                    $$.ajax({
                        url: "/rest/IotCalcBusiness/remove",
                        data: {
                            id: rowIds.join(',')
                        },
                        success: function () {
                            refresh();
                        }
                    });
                    layer.close(index);
                });
        }

    });




})

function classifyOpt() {
	$("#authorityTable").on("mouseover mouseout","tr",function(event){
	  	console.log(event.type);
		if(event.type == 'mouseover'){
			$(this).find('.rt').removeClass('hidden');
		}else{
			$(this).find('.rt').addClass('hidden');
		}
	});
}

 



//新建分类弹出框
function addClassify(e, data) {
   if(data==-1 || data==-9999){
       data=0;
   }
    e = e || window.event;
    layer.open({
        move: false,
        type: 2,
        title: '新建分类',
        area: ['500px', '200px'],
        content: 'calc_catelog_add.html?rowId=' + data,
        btn: ['确定', '取消'],
        btnAlign: 'c',
        yes: function (index, layero) {


            var openWin = window[layero.find('iframe')[0]['name']]; //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
            var ret = openWin.yesOperator();
            if (ret) {
                layer.close(index);
                refreshTree();
            }
        }, cancel: function (index) {
            layer.close(index);
        }
    });
    e.stopPropagation();
}

//编辑分类弹出框
function editClassify(e, data) {

    e = e || window.event;
    layer.open({
        move: false,
        type: 2,
        title: '编辑分类',
        area: ['500px', '200px'],
        content: 'calc_catelog_update.html?rowId=' + data,
        btn: ['确定', '取消'],
        btnAlign: 'c',
        yes: function (index, layero) {
            var openWin = window[layero.find('iframe')[0]['name']]; //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
            var ret = openWin.yesOperator();
            if (ret) {
                layer.close(index);
                refreshTree();
            }
        }, cancel: function (index) {
            layer.close(index);
        }
    });
    e.stopPropagation();
}

//删除分类弹出框
function delClassify(e, data) {
    e = e || window.event;
    layer.confirm('确定要删除分类吗？',
        {icon: 3, title: '提示', area: '300px', btnAlign: 'c'}, function (index) {

            $$.ajax({
                url: "/rest/IotCalcCatalogBusiness/remove",
                data: {
                    id: data
                },
                success: function () {
                    layer.close(index);
                    refreshTree();
                }
            });

        });
    e.stopPropagation();
}




